package org.hgvc.webapp.ets.jsf.beans.admin;

import java.util.List;

import javax.faces.component.UIParameter;
import javax.faces.event.ActionEvent;
import javax.faces.model.SelectItem;

import org.hgvc.webapp.ets.bo.Member;
import org.hgvc.webapp.ets.bo.Membership;
import org.hgvc.webapp.ets.spring.service.MembershipService;
import org.jocsb.base.AbstractBean;
import org.jocsb.base.exception.BaseException;
import org.jocsb.base.util.FacesUtils;


public class AdminSelectMembershipBean extends AbstractBean{

	public static String BEAN_NAME="adminSelectMembershipBean";
	private MembershipService service;
	private List<Membership>membershipList;
	public static String NAV_SELECT_ACTION="adminSelectProspectMembership";
	private String action;
	private List<SelectItem> membershipSelectList;
	private Membership membership;
	private boolean newAgreement=false;
	private Member member;
	
	public boolean isNewAgreement() {
		return newAgreement;
	}
	
	public void setNewAgreement(boolean newAgreement) {
		this.newAgreement = newAgreement;
	}

	public void setService(MembershipService service) {
		this.service = service;
	}
	/*-----------------------------------------------------*/

	/*public List<SelectItem> getMembershipSelectList() {
		try{
			membershipList = service.getMembershipList();
			membershipSelectList = new ArrayList<SelectItem>();
			for(Membership membership:membershipList){
				SelectItem item = new SelectItem(membership.getMembershipId(),membership.getName());
				membershipSelectList.add(item);
			}
		}catch(BaseException be){
			be.printStackTrace();
			initErrorBean(be);
		}
		return membershipSelectList;
	}*/


	public String navIndSelectAction(){
		newAgreement=false;
		action = AdminCreateIndProspectBean.NAV_CREATION;
		return NAV_SELECT_ACTION.concat("_"+getRoleName());
	}

	public String navCompSelectAction(){
		action = AdminCreateCompProspectBean.NAV_CREATE;
		return NAV_SELECT_ACTION.concat("_"+getRoleName());
	}

	public void selectActionEvent(ActionEvent actionEvent){
		UIParameter param = (UIParameter)actionEvent.getComponent().findComponent("membershipId");
		String id = (String)param.getValue();
		for(Membership membership:membershipList){
			if(membership.getMembershipId().equalsIgnoreCase(id)){
				this.membership = membership;
				break;
			}
		}
	}

	public String navProspectCreation(){
		try{
			if (!newAgreement){
				if(action.equalsIgnoreCase(AdminCreateIndProspectBean.NAV_CREATION)){
					FacesUtils.resetManagedBean(AdminProspectPaymentBean.BEAN_NAME);
					AdminCreateIndProspectBean bean = (AdminCreateIndProspectBean)FacesUtils.getManagedBean(AdminCreateIndProspectBean.BEAN_NAME);
					bean.setMembership(membership);
					
					return bean.navCreateAction();
				}else{
					FacesUtils.resetManagedBean(AdminProspectPaymentBean.BEAN_NAME);
					AdminCreateCompProspectBean bean = (AdminCreateCompProspectBean)FacesUtils.getManagedBean(AdminCreateCompProspectBean.BEAN_NAME);
					bean.setMembership(membership);
					return bean.navCreateAction();
				}
			}else{
				AdminProspectPaymentBean bean = (AdminProspectPaymentBean)FacesUtils.getManagedBean(AdminProspectPaymentBean.BEAN_NAME);
				 
				bean.setMember(member); 
				bean.setMembership(membership);
				bean.setNewAgreement(true);
				return bean.navSelectOptionAction();	
			}
			
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return null;
	}

	public List<Membership> getMembershipList() {
		try{
			membershipList = service.getMembershipList();
		}catch(BaseException be){
			be.printStackTrace();
			initErrorBean(be);
		}

		return membershipList;
	}

	public Member getMember() {
		return member;
	}

	public void setMember(Member member) {
		this.member = member;
	}

	public String getAction() {
		return action;
	}

	public void setAction(String action) {
		this.action = action;
	}

}
